//
//  HDAddressBookTool.m
//  宝宝玩儿
//
//  Created by 小赵 on 16/4/22.
//  Copyright © 2016年 小赵. All rights reserved.
//
@class HDAddressBookTool;
static HDAddressBookTool * tool;
#import "HDAddressBookTool.h"
#import "HDAddressBookPersonModel.h"
@implementation HDAddressBookTool
{
    ABAddressBookRef addressBook;
}
+(HDAddressBookTool*)shareTool
{
    if(!tool)
    {
        tool = [[HDAddressBookTool alloc] init];
    }
    return tool;
}
-(void)initialAddressBookSuccess:(void(^)(NSArray * addressBook))success
{
    if(!addressBook)
    addressBook = ABAddressBookCreate();
    //取得授权状态
    ABAuthorizationStatus authStatus =
    ABAddressBookGetAuthorizationStatus();
    
    if (authStatus != kABAuthorizationStatusAuthorized)
    {
        ABAddressBookRequestAccessWithCompletion
        (addressBook, ^(bool granted, CFErrorRef error)
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (error)
                     NSLog(@"Error: %@", (__bridge NSError *)error);
                 else if (!granted) {
                     UIAlertView *av = [[UIAlertView alloc]
                                        initWithTitle:@"Authorization Denied"
                                        message:@"Set permissions in Settings>General>Privacy."
                                        delegate:nil
                                        cancelButtonTitle:nil
                                        otherButtonTitles:@"OK", nil, nil];
                     [av show];
                 }
                 else
                 {
                     //还原 ABAddressBookRef
                     NSArray * contentArr = [self getAddressBook:addressBook];
                     success(contentArr);
                 }
             });  
         });  
    }
    else
    {
        //还原 ABAddressBookRef
        NSArray * contentArr = [self getAddressBook:addressBook];
        success(contentArr);
    }
    

}
-(NSArray*)getAddressBook:(ABAddressBookRef)Book{
    //获取所有联系人的数组
    CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(Book);
    //获取联系人总数
    CFIndex number = ABAddressBookGetPersonCount(Book);
    NSMutableArray * personMdoelArr = [NSMutableArray array];
     for (NSInteger i=0; i<number; i++) {
        HDAddressBookPersonModel * personModel = [[HDAddressBookPersonModel alloc] init];
        //获取联系人对象的引用
        ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);
        //获取当前联系人名字
        personModel.firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
        //获取当前联系人姓氏
        personModel.lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
         personModel.contactName = (personModel.lastName.length&&personModel.firstName)?[NSString stringWithFormat:@"%@%@",personModel.lastName,personModel.firstName]:(personModel.lastName.length?personModel.lastName:personModel.firstName);
        //获取当前联系人中间名
        personModel.middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
        //获取当前联系人的名字前缀
        personModel.prefixName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty));
        //获取当前联系人的名字后缀
        personModel.suffixName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty));
        //获取当前联系人的昵称
       personModel.nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));
        //获取当前联系人的电话 数组
        NSMutableArray * phoneArr = [[NSMutableArray alloc]init];
        ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);
        for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {
            NSString * number =(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j));
//            NSLog(@"%@",number);
            number = [number stringByReplacingOccurrencesOfString:@"+86 " withString:@""];
            number = [number stringByReplacingOccurrencesOfString:@"-" withString:@""];
            [phoneArr addObject:number];
        }
         personModel.contactTel = phoneArr.count?phoneArr.firstObject:@" ";
         personModel.contactName = personModel.contactName.length?personModel.contactName:personModel.contactTel;
        [personMdoelArr addObject:personModel];
    }
    return personMdoelArr;
}

@end
